package com.itheima.web.controller;

import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.domain.Catalog;
import com.itheima.domain.Company;
import com.itheima.domain.Result;
import com.itheima.factory.BeanFactory;
import com.itheima.service.CatalogService;
import com.itheima.utils.BeanUtil;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/store/catalog/*")
public class CatalogServlet extends BaseServlet {
    CatalogService catalogService = BeanFactory.getBean("catalogService",CatalogService.class);
    private Result delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取请求参数并封装
        try {
            String[] ids = BeanUtil.fillBeanFromJson(request, String[].class);
            //调用service层edit方法,执行删除操作
            catalogService.delete(ids);
            //响应客户端
            return new Result(true,MessageConstant.DELETE_CATALOG_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.DELETE_CATALOG_FAIL);
        }
    }

    private Result edit(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取请求参数并封装
        try {
            Catalog catalog = BeanUtil.fillBeanFromJson(request, Catalog.class);
            //调用service层edit方法,执行保存操作
            catalogService.edit(catalog);
            //响应客户端
            return new Result(true,MessageConstant.EDIT_CATALOG_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.EDIT_CATALOG_FAIL);
        }
    }

    private Result findById(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            //1.获取请求参数
            String catalogId = request.getParameter("id");
            //调用service层方法,获取查询结果
            Catalog catalog = catalogService.findById(catalogId);
            //对客户端做出响应
            return new Result(true, MessageConstant.QUERY_CATALOG_SUCCESS,catalog);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CATALOG_FAIL);
        }
    }

    private Result save(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取请求参数并封装
        try {
            Catalog catalog = BeanUtil.fillBeanFromJson(request, Catalog.class);
            //调用service层save方法,执行保存操作
            catalogService.save(catalog);
            //响应客户端
            return new Result(true,MessageConstant.SAVE_CATALOG_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.SAVE_CATALOG_FAIL);
        }
    }

    //处理分页查询的请求
    private Result findByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            //1.获取请求参数
            String strCurrentPage = request.getParameter("currentPage");
            String strPageSize = request.getParameter("pageSize");
            //1.1判断非空
            int currentPage = 1,pageSize = 5;
            if(!StringUtils.isBlank(strCurrentPage)){
                currentPage = Integer.parseInt(strCurrentPage);
            }
            if(!StringUtils.isBlank(strPageSize)){
                pageSize = Integer.parseInt(strPageSize);
            }
            //调用service层方法,获取查询结果
            PageInfo<Catalog> pageInfo = catalogService.findByPage(currentPage,pageSize);
            //对客户端做出响应
            return new Result(true, MessageConstant.QUERY_CATALOG_SUCCESS,pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CATALOG_FAIL);
        }
    }

    private Result findAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            List<Catalog> list = catalogService.findAll();
            //对客户端做出响应
            return new Result(true, MessageConstant.QUERY_COURSE_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_COURSE_FAIL);
        }
    }

}
